﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.Mapping;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// RoleContainPermissions 类。
	/// </summary>
	internal static partial class RoleContainPermissions
	{
		#region GetItem
		/// <summary>
		/// 根据标识获取指定的角色权限。
		/// </summary>
		/// <param name="o">角色权限集合。</param>
		/// <param name="roleId">获取的角色权限标识 roleId。</param>
		/// <param name="resourceId">获取的角色权限标识 resourceId。</param>
		/// <param name="permissionCode">获取的角色权限标识 permissionCode。</param>
		/// <returns>获取的信息项。</returns>
		public static RoleContainPermission GetItem(this System.Data.Linq.Table<RoleContainPermission> o, Guid roleId, Guid resourceId, int permissionCode)
		{
			return o.SingleOrDefault(p => p.RoleId == roleId && p.ResourceId == resourceId && p.PermissionCode == permissionCode);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的角色权限是否已经存在。
		/// </summary>
		/// <param name="o">角色权限集合。</param>
		/// <param name="roleId">获取的角色权限标识 roleId。</param>
		/// <param name="resourceId">获取的角色权限标识 resourceId。</param>
		/// <param name="permissionCode">获取的角色权限标识 permissionCode。</param>
		/// <returns>布尔值表示指定的角色权限是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<RoleContainPermission> o, Guid roleId, Guid resourceId, int permissionCode)
		{
			return o.Any(p => p.RoleId == roleId && p.ResourceId == resourceId && p.PermissionCode == permissionCode);
		}
		#endregion
	}




	/// <summary>
	/// RoleContainPermissions 类操作部分。
	/// </summary>
	internal static partial class RoleContainPermissions
	{
		#region Delete
		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, RoleContainPermission rcp)
		{
			dc.RoleContainPermissions.DeleteOnSubmit(rcp);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, Permission parent)
		{
			IEnumerable<RoleContainPermission> e = parent.RoleContainPermissions.AsEnumerable();
			foreach (RoleContainPermission i in e)
				Delete(dc, i);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, Role parent)
		{
			IEnumerable<RoleContainPermission> e = parent.ContainPermissions.AsEnumerable();
			foreach (RoleContainPermission i in e)
				Delete(dc, i);
		}
		#endregion
		#endregion
	}

	public class PermissionState
	{
		[Column(Storage = "_ResourceId", DbType = "UniqueIdentifier NOT NULL", IsPrimaryKey = true)]
		public Guid ResourceId { get; set; }
		[Column(Storage = "_PermissionCode", DbType = "Int NOT NULL", IsPrimaryKey = true)]
		public int PermissionCode { get; set; }
		[Column(Storage = "_IsEnabled", DbType = "Bit NOT NULL")]
		public bool IsEnabled { get; set; }
	}
}